package cn.xfdzx.android.apps.shop.adapter;

import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.bean.HomeSearchRetailGoodsBean;
import cn.xfdzx.android.apps.shop.bean.HomeSearchXFDGoodBeen;
import cn.xfdzx.android.apps.shop.bean.SearchShopBean;
import cn.xfdzx.android.apps.shop.contract.GoodActionCallback;
import cn.xfdzx.android.apps.shop.presenter.HomeSearchPresenter;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;
import cn.xfdzx.android.apps.shop.util.Utils;
import cn.xfdzx.android.apps.shop.widget.CornerImageView;

/**
 * Creator: zf.
 * Date: 2020/10/27.
 * Description:零售-搜索商品列表适配器.
 */
public class SearchGoodsListRetailAdapter extends BaseQuickAdapter<HomeSearchRetailGoodsBean.DataBean.GoodsBean, BaseViewHolder>  {
    private List<HomeSearchRetailGoodsBean.DataBean.GoodsBean> goods = new ArrayList<>();
    private FlexboxLayoutManager manager;
    private SearchGoodsListRetailLableAdapter listRetailLableAdapter;
    private HomeSearchPresenter mPresenter;
    private OnItemAddCartListener onItemAddCartListener;
    private GoodActionCallback callback;
    public SearchGoodsListRetailAdapter(@Nullable List<HomeSearchRetailGoodsBean.DataBean.GoodsBean> data, HomeSearchPresenter mPresenter) {
        super(R.layout.item_search_goodsretail, data);
        this.mPresenter = mPresenter;
    }

    @Override
    public void setData(int index, @NonNull HomeSearchRetailGoodsBean.DataBean.GoodsBean data) {
        super.setData(index, data);
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final HomeSearchRetailGoodsBean.DataBean.GoodsBean date) {
        CornerImageView img_goods = viewHolder.getView(R.id.img_goods);
        img_goods.setType(CornerImageView.TYPE_ROUND);
        ImageView img_cart = viewHolder.getView(R.id.img_cart);
        CornerImageView img_shop = viewHolder.getView(R.id.img_shop);
        img_shop.setType(CornerImageView.TYPE_CIRCLE);
        TextView tv_goshops = viewHolder.getView(R.id.tv_goshops);
        TextView tv_goods_oldprice = viewHolder.getView(R.id.tv_goods_oldprice);
        RecyclerView recycle_lable = viewHolder.getView(R.id.recycle_lable);
        TextView tv_goodscount = viewHolder.getView(R.id.tv_goodscount);

        if (EmptyUtil.isNotEmpty(date.getLabel())){
            //店铺标签流式布局
            manager = new FlexboxLayoutManager(mContext);
            //设置主轴排列方式
            manager.setFlexDirection(FlexDirection.ROW);
            //设置是否换行
            manager.setFlexWrap(FlexWrap.WRAP);
            recycle_lable.setLayoutManager(manager);

            listRetailLableAdapter = new SearchGoodsListRetailLableAdapter(date.getLabel());
            recycle_lable.setAdapter(listRetailLableAdapter);
        }
        if (date.getCartCount()==0){
            tv_goodscount.setVisibility(View.GONE);
        }else {
            tv_goodscount.setVisibility(View.VISIBLE);
            tv_goodscount.setText(date.getCartCount()+"");
        }

        viewHolder.setText(R.id.tv_goodsname,date.getTitle())
                .setText(R.id.tv_shopsname,date.getOffline_shop_name());

        if (Double.parseDouble(date.getDiscountprice()) == 0) {
            viewHolder.setText(R.id.tv_goods_newprice,"¥"+date.getMarketprice());
        } else {
            viewHolder.setText(R.id.tv_goods_newprice,"¥"+date.getDiscountprice());
        }
        viewHolder.setText(R.id.tv_goods_oldprice,"¥"+date.getMarketprice());
        tv_goods_oldprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
        if (null != date.getThumb()) {
            ImageGlideUtil.loadImage(mContext,img_goods,date.getThumb());
        }
        if (null != date.getLogo()) {
            ImageGlideUtil.loadImage(mContext,img_shop,date.getLogo());
        }

        img_cart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    onItemAddCartListener.onItemAddCartClick(viewHolder.getLayoutPosition(),date,img_cart);

                }
            }
        });

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    Intent intent = new Intent(mContext, ShopActivity.class);
                    intent.putExtra("shop_id", date.getRole_id() + "");
                    mContext.startActivity(intent);

                }
            }
        });
//        tv_goshops.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (Utils.isFastClick()) {
//                    Intent intent = new Intent(mContext, ShopActivity.class);
//                    intent.putExtra("shop_id", date.getRole_id() + "");
//                    mContext.startActivity(intent);
//                }
//            }
//        });
    }
    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<HomeSearchRetailGoodsBean.DataBean.GoodsBean> list){
        super.replaceData(list);
    }

    public void setItemClickListener(OnItemAddCartListener listener){
        this.onItemAddCartListener = listener;
    }

    public interface OnItemAddCartListener {
        void onItemAddCartClick(int position,HomeSearchRetailGoodsBean.DataBean.GoodsBean date,ImageView img_cart);
    }
}
